import {Component, OnInit, Injector} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {NzModalService, NzMessageService} from 'ng-zorro-antd';

import {CreateTenantModalComponent} from './create-tenant-modal.component';
import {EditTenantModalComponent} from './edit-tenant-modal.component';

import {appModuleAnimation} from '@shared/animations/routerTransition';

import {AppModalService} from '@shared/app-modal.service';

import {
    TenantServiceProxy,
    TenantListDto,
    NameValueDto,
    CommonLookupServiceProxy,
    FindUsersInput,
    EntityDtoOfInt64
} from '@shared/service-proxies/service-proxies';

import {MessageService} from '@core/net/message/MessageService';

import {AppComponentBase} from '@shared/app-component-base';

import * as moment from 'moment';

@Component({selector: 'app-tenant-list', templateUrl: './tenant-list.component.html', styles: []})
export class TenantListComponent extends AppComponentBase implements OnInit {

    pageIndex = 1;
    pageSize = 10;
    total = 200; // mock total
    list = [];
    loading = false;
    args: any = {};
    _indeterminate = false;
    _allChecked = false;

    filters: {
        filterText: string;
        creationDateRangeActive: boolean;
        subscriptionEndDateRangeActive: boolean;
        subscriptionEndDateStart: moment.Moment;
        subscriptionEndDateEnd: moment.Moment;
        subscriptionEndDateRange: [null, null];
        creationDateStart: moment.Moment;
        creationDateEnd: moment.Moment;
        creationDateRange: [null, null];
        selectedEditionId: number;
    } = <any>{};

    constructor(injector: Injector, private _tenantService: TenantServiceProxy, private _activatedRoute : ActivatedRoute, private modal : NzModalService, private appModal : AppModalService) {
        super(injector);
        this.setFiltersFromRoute();
}

        setFiltersFromRoute(): void {if (this._activatedRoute.snapshot.queryParams['subscriptionEndDateStart'] != null) {
            this.filters.subscriptionEndDateRangeActive = true;
            this.filters.subscriptionEndDateStart = moment(this._activatedRoute.snapshot.queryParams['subscriptionEndDateStart']);
        } else {
            this.filters.subscriptionEndDateStart = moment().startOf('day');
        }

        if (this._activatedRoute.snapshot.queryParams['subscriptionEndDateEnd'] != null) {
            this.filters.subscriptionEndDateRangeActive = true;
            this.filters.subscriptionEndDateEnd = moment(this._activatedRoute.snapshot.queryParams['subscriptionEndDateEnd']);
        } else {
            this.filters.subscriptionEndDateEnd = moment()
                .add(30, 'days')
                .endOf('day');
        }

        if (this._activatedRoute.snapshot.queryParams['creationDateStart'] != null) {
            this.filters.creationDateRangeActive = true;
            this.filters.creationDateStart = moment(this._activatedRoute.snapshot.queryParams['creationDateStart']);
        } else {
            this.filters.creationDateStart = moment()
                .add(-7, 'days')
                .startOf('day');
        }

        if (this._activatedRoute.snapshot.queryParams['creationDateEnd'] != null) {
            this.filters.creationDateRangeActive = true;
            this.filters.creationDateEnd = moment(this._activatedRoute.snapshot.queryParams['creationDateEnd']);
        } else {
            this.filters.creationDateEnd = moment().endOf('day');
        }
}

        ngOnInit() {this.getTenants();
}

        load(pi?: number){if (typeof pi !== 'undefined') {
            this.pageIndex = pi || 1;
        }
        console.log(this.pageIndex);
        this.getTenants();
}
        resetSearch(){this.setFiltersFromRoute();
        this.load();
}

        getTenants() {this
            ._tenantService
            .getTenants(this.filters.filterText, this.filters.subscriptionEndDateRangeActive
                ? this.filters.subscriptionEndDateStart
                : undefined, this.filters.subscriptionEndDateRangeActive
                ? this.filters.subscriptionEndDateEnd
                : undefined, this.filters.creationDateRangeActive
                ? this.filters.creationDateStart
                : undefined, this.filters.creationDateRangeActive
                ? this.filters.creationDateEnd
                : undefined, this.filters.selectedEditionId, this.filters.selectedEditionId !== undefined && (this.filters.selectedEditionId + '') !== '-1', '', this.pageSize, (this.pageIndex - 1) * this.pageSize)
            .subscribe(result => {
                console.log(result);
                this.list = result.items;
                this.total = result.totalCount;
            });
}

        createTenant(): void {this.showModal(CreateTenantModalComponent, {
            tenancyId: null
        }, () => {
            this.load();
        });
}

        editedTenant(tenantId: number): void {this.showModal(EditTenantModalComponent, {
            tenancyId: tenantId
        }, () => {
            this.load();
        });
}

        deleteTenant(tenant: TenantListDto): void {
            this
            .message
            .confirm(this.l('TenantDeleteWarningMessage', tenant.tenancyName), isConfirmed => {
                if (isConfirmed) {
                    this
                        ._tenantService
                        .deleteTenant(tenant.id)
                        .subscribe(() => {
                            this.load();
                            this
                                .notify
                                .success(this.l('SuccessfullyDeleted'));
                        });
                }
            });
        }
}
